<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class Hd_holiday_lottery_history extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hdholidaycustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_hd_holiday_gift_type,$g_hd_holiday_type;
        $data['game_gift'] = array4js($g_hd_holiday_gift_type);
        $data['type_data'] = array4js($g_hd_holiday_type);

        $this->load->view('admin/hd_holiday_lottery_history',$data);
    }

    public function getLotteryHistoryList() {
        $assist['where'] = getFilter($this->input->post(),Hdholidaycustomermodel::$historySearchField);
        $assist['like'] = getFilter($this->input->post(),Hdholidaycustomermodel::$historyLikeField);
        $assist['pagination'] = getPagination($this->input->post());

        $info = $this->input->post();
        if(isset($info['gift_status']) && $info['gift_status'] == 0) {
            $assist['where']['hd_holiday_lottery_history.gift_type >'] ='0';
        }

        if(isset($assist['where']['hd_holiday_lottery_history.gift_type']) && $assist['where']['hd_holiday_lottery_history.gift_type'] < 0) {
            unset($assist['where']['hd_holiday_lottery_history.gift_type']);
            $assist['where']['hd_holiday_lottery_history.gift_type >'] ='0';
        }

        $assist['select'] = '
            hd_holiday_customer.customer_id,
            hd_holiday_customer.openid,
            hd_holiday_lottery_history.*
        ';
        list($list,$count) = $this->m_customer->getLotteryHistoryListSection($assist);

        // foreach ($list as $key => $value) {
        //     $list[$key]['nickname'] = urldecode($value['nickname']);
        // }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function exportList() {
        @set_time_limit(0);
        @ini_set('memory_limit', '3048M');

        $info = $this->input->get();
        $assist['where'] = getFilter($this->input->get(),Hdholidaycustomermodel::$historySearchField);
        $assist['like'] = getFilter($this->input->get(),Hdholidaycustomermodel::$historyLikeField);

        $info = $this->input->post();
        if(isset($info['gift_status']) && $info['gift_status'] == 0) {
            $assist['where']['hd_holiday_lottery_history.gift_type >'] ='0';
        }

        if(isset($assist['where']['hd_holiday_lottery_history.gift_type']) && $assist['where']['hd_holiday_lottery_history.gift_type'] < 0) {
            unset($assist['where']['hd_holiday_lottery_history.gift_type']);
            $assist['where']['hd_holiday_lottery_history.gift_type >'] ='0';
        }


        $assist['select'] = '
            hd_holiday_customer.customer_id,
            hd_holiday_customer.openid,
            hd_holiday_lottery_history.*
        ';
        list($list,$count) = $this->m_customer->getLotteryHistoryListSection($assist);
        if(count($list) > 8000) {
            die('导出数据太大，请筛选数据导出!');
        }

        $this->load->library('excel');
        $this->excel->init('广汽本田游戏用户抽奖历史_'.date('YmdH').'.xls');
        $this->excel->addSheet('广汽本田游戏用户抽奖历史');
        $this->excel->addTitle(array(
            array('content'=>'顾客ID'),
            array('content'=>'openid'),
            array('content'=>'姓名'),
            array('content'=>'手机'),
            array('content'=>'奖品'),
            array('content'=>'奖品状态'),
            array('content'=>'抽奖时间'),
            array('content'=>'领取时间'),
            array('content'=>'抽奖来源'),
        ));

        global $g_hd_holiday_game_gift,$g_hd_holiday_card_type,$g_hd_holiday_gift_type;

        $sex_array = array(
            0 => '未知',
            1 => '男',
            2 => '女',
        );
        foreach ($list as $key => $value) {
            $this->excel->addRow(array(
                array('content' => $value['customer_id']),
                array('content' => $value['openid']),
                array('content' => $value['customer_name']),
                array('content' => $value['customer_phone']),
                array('content' => id2name($value['gift_type'],$g_hd_holiday_gift_type)),
                array('content' => $value['gift_status'] == 0 ? '未核销' : '已核销'),
                array('content' => $value['lottery_time_time_create']),
                array('content' => $value['lottery_time_time_active']),
                array('content' => $value['lottery_source']),
            ));
            unset($list[$key]);
        }

        $this->excel->freezePanes(1,0);
        $this->excel->generate();
    }

}
/* End of file aa_game_history.php */
/* Location: ./application/controllers/aa_game_history.php */
